package com.hzeport.bgs22emduty.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzeport.bgs22emduty.config.login.LoginCompoentService;
import com.hzeport.bgs22emduty.config.login.UserInfoDetailVO;
import com.hzeport.bgs22emduty.dto.ScreenWaitListDTO;
import com.hzeport.bgs22emduty.dto.ScreenWaitListSaveDTO;
import com.hzeport.bgs22emduty.dto.ScreenWaitListUpdateDTO;
import com.hzeport.bgs22emduty.entity.ScreenNotification;
import com.hzeport.bgs22emduty.outbound.ScreenWaitListRepository;
import com.hzeport.bgs22emduty.vo.ScreenWaitList.ScreenWaitListVO;
import com.hzeport.bgs22emduty.common.base.BaseServiceImpl;
import com.hzeport.bgs22emduty.entity.ScreenWaitList;
import com.hzeport.bgs22emduty.outbound.db.ScreenWaitListRepositoryImpl;
import com.hzeport.bgs22emduty.service.ScreenWaitListService;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * ${comments}
 *
 * @author code generator
 * @date 2023-02-14 09:39:44
 */
@Service
@Slf4j
public class ScreenWaitListServiceImpl extends BaseServiceImpl<ScreenWaitListRepositoryImpl, ScreenWaitList> implements ScreenWaitListService {

    @Resource
    private ScreenWaitListRepository screenWaitListRepository;

    @Resource
    private LoginCompoentService loginCompoentService;

    @Override
    public Object page(ScreenWaitListDTO dto) {
        UserInfoDetailVO userInfoDetailVO = loginCompoentService.getUserInfo();
        LambdaQueryWrapper<ScreenWaitList> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(userInfoDetailVO.getCustomscode()),ScreenWaitList::getCustomId,userInfoDetailVO.getCustomscode());
        queryWrapper.like(StringUtils.isNotBlank(dto.getKeyWord()),ScreenWaitList::getMainBody,dto.getKeyWord());
        if (!CollectionUtils.isEmpty(dto.getList()) && StringUtils.isNotBlank(dto.getList().get(0))){
            LocalDateTime startLocalDateTime = LocalDate.parse(dto.getList().get(0), DateTimeFormatter.ofPattern("yyyy-MM-dd")).atStartOfDay();
            queryWrapper.ge(ScreenWaitList::getCreateTime,startLocalDateTime);
        }
        if (!CollectionUtils.isEmpty(dto.getList()) && StringUtils.isNotBlank(dto.getList().get(1))){
            LocalDateTime endLocalDateTime = LocalDate.parse(dto.getList().get(1), DateTimeFormatter.ofPattern("yyyy-MM-dd")).atStartOfDay().plusDays(1);
            queryWrapper.le(ScreenWaitList::getCreateTime,endLocalDateTime);
        }
        queryWrapper.orderByDesc(ScreenWaitList::getCreateTime);
        Page<ScreenWaitList> page = screenWaitListRepository.page(new Page<>(dto.getPageNum(), dto.getPageSize()), queryWrapper);
        List<ScreenWaitListVO> dateOfHandling = page.getRecords().stream().map(screenWaitList -> {
            ScreenWaitListVO screenWaitListVO = new ScreenWaitListVO();
            BeanUtil.copyProperties(screenWaitList, screenWaitListVO, "dateOfHandling");
            screenWaitListVO.setDateOfHandling(screenWaitList.getDateOfHandling().toLocalDate());
            return screenWaitListVO;
        }).collect(Collectors.toList());
        Page<ScreenWaitListVO> screenWaitListVOPage = new Page<>();
        BeanUtil.copyProperties(page,screenWaitListVOPage,"records");
        screenWaitListVOPage.setRecords(dateOfHandling);
        return screenWaitListVOPage;
    }

    @Override
    public Boolean save(ScreenWaitListSaveDTO dto) {
        UserInfoDetailVO userInfoDetailVO = loginCompoentService.getUserInfo();
        log.info("用户信息："+userInfoDetailVO);
        ScreenWaitList screenWaitList = new ScreenWaitList();
        BeanUtil.copyProperties(dto,screenWaitList,"dateOfHandling");
        screenWaitList.setDateOfHandling(LocalDate.parse(dto.getDateOfHandling(), DateTimeFormatter.ofPattern("yyyy-MM-dd")).atStartOfDay());
        screenWaitList.setCreatePerson(userInfoDetailVO.getUserName());
        screenWaitList.setCustomId(userInfoDetailVO.getCustomscode());
        screenWaitList.setCustomName(userInfoDetailVO.getCustomsName());
        return screenWaitListRepository.save(screenWaitList);
    }

    @Override
    public Boolean updateById(ScreenWaitListUpdateDTO dto) {
        ScreenWaitList screenWaitList = new ScreenWaitList();
        BeanUtil.copyProperties(dto,screenWaitList,"dateOfHandling");
        screenWaitList.setDateOfHandling(LocalDate.parse(dto.getDateOfHandling(),DateTimeFormatter.ofPattern("yyyy-MM-dd")).atStartOfDay());
        if (StringUtils.isNotBlank(dto.getId())) {
            ScreenWaitList byId = screenWaitListRepository.getById(dto.getId());
            if (byId != null){
                screenWaitList.setRecVersion(byId.getRecVersion());
            }
        }
        return screenWaitListRepository.updateById(screenWaitList);
    }

    @Override
    public ScreenWaitListVO detailById(String id) {
        ScreenWaitList byId = screenWaitListRepository.getById(id);
        ScreenWaitListVO screenWaitListVO = new ScreenWaitListVO();
        BeanUtil.copyProperties(byId,screenWaitListVO,"dateOfHandling");
        screenWaitListVO.setDateOfHandling(byId.getDateOfHandling().toLocalDate());
        return screenWaitListVO;
    }
}
